package com.pgcomb.poweriot.alarmservice.resource;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pgcomb.common.bean.AlarmData;
import com.pgcomb.common.bean.AlarmParam;
import com.pgcomb.common.bean.ResultBean;
import com.pgcomb.poweriot.alarmservice.alarm.service.MqttAlarmService;
import com.pgcomb.poweriot.alarmservice.vo.AlarmMqtt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * @author zhengheqi
 * @version 1.0.0
 * @Description: TODO
 * @Date 2021/6/5 20:27
 */
@Slf4j
@RestController
@RequestMapping("test")
public class Test {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Resource
    MqttAlarmService mqttAlarmService;

    @PostMapping("sendAlarm")
    public ResultBean<?> sendAlarm(@RequestBody AlarmMqtt alarmMqtt){

        mqttAlarmService.send(alarmMqtt);

        return ResultBean.ok();
    }

    @GetMapping("test")
    public String sendMsg(String msg) throws JsonProcessingException {

        AlarmData alarmData = new AlarmData();
        alarmData.setSbId("12");
        alarmData.setTime(LocalDateTime.now());
        alarmData.setType("data");

        AlarmParam alarmParam = new AlarmParam();

        alarmParam.setAlarmLevel("warn");
        alarmParam.setField("ua");
        alarmParam.setName("name");
        alarmParam.setValue(12);
        alarmParam.setType("gt");
        alarmParam.setThreshold(1);

        ArrayList<AlarmParam> objects = new ArrayList<>();
        objects.add(alarmParam);

        alarmData.setParam(objects);

        rabbitTemplate.convertAndSend("sendmsg", alarmData);


        return "s";
    }
}
